package com.wen.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.commonutils.GuLiUtils;
import com.wen.commonutils.ordervo.OrderCourseInfoVO;
import com.wen.commonutils.ordervo.OrderUcenterMember;
import com.wen.order.api.EduApi;
import com.wen.order.api.UcenterApi;
import com.wen.order.entity.TOrder;
import com.wen.order.mapper.TOrderMapper;
import com.wen.order.service.TOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author wen
 * @since 2021-06-19
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
    @Resource
    private EduApi eduApi;
    @Resource
    private UcenterApi ucenterApi;

    /**
     * 根据课程id创建订单
     *
     * @param courseId 课程id
     * @param memberId 用户id
     * @return 订单号
     */
    @Override
    public String createOrder(String courseId, String memberId) {
//        根据用户id远程调用获取用户信息的接口
        OrderUcenterMember ucenterMember = ucenterApi.getMemberInfoById(memberId);
//        根据课程id远程调用获取课程信息的接口
        OrderCourseInfoVO orderCourseInfo = eduApi.getOrderCourseInfo(courseId);
//        添加订单信息
        TOrder order = new TOrder();
        order.setCourseId(orderCourseInfo.getId());//课程id
        order.setCourseTitle(orderCourseInfo.getTitle());//课程名
        order.setCourseCover(orderCourseInfo.getCover());//课程封面
        order.setTotalFee(orderCourseInfo.getPrice());//课程价格
        order.setTeacherName(orderCourseInfo.getTeacherName());//讲师姓名
        order.setMemberId(ucenterMember.getId());//用户id
        order.setMobile(ucenterMember.getMobile());//用户手机
        order.setNickname(ucenterMember.getNickname());//用户昵称
//        根据当前时间生成唯一的的订单号
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = dateFormat.format(new Date(System.currentTimeMillis()));
        String orderNo = time + GuLiUtils.getSixBitRandom();
        order.setOrderNo(orderNo);
        order.setPayType(1);// 订单支付类型，微信-1，支付宝-2
        order.setStatus(0);//订单状态（0：未支付，1：已支付）
//        完成订单添加
        baseMapper.insert(order);
//        返回订单号
        return orderNo;
    }
}
